(function( $ ){
    var methods = {
    	init : function(options){
    	 	settings = {
    	 		'addInput': false,
    	 		'addInputAtEnd': false,
    	 		'inputId': 'gFilterInput',
    	 		'customInputClass': null,
    	 		'filter': 'li',
    	 		'displayMsg': true,
    	 		'msg': 'Nothing found',
    	 		'animate': false,
    	 		'colorResults': false //no idea hayet3emel ezay =D
    	 	};
    	 	
    	 	if(options)
    	 		$.extend(settings, options);
    	 	
    	 	if(settings['addInput']){
    	 		var custom_input_class = settings['customInputClass'];
    	 		if(!(settings['addInputAtEnd'])){
    	 			if(custom_input_class != null){
    	 				this.html("<input id='gFilterInput' type='text' class='"+custom_input_class+"'/>"+this.html());
    	 			}
    	 			else{
    	 				this.html("<input id='gFilterInput' type='text'/>"+this.html());
    	 			}
    	 		}
    	 		else{
    	 			if(custom_input_class != null){
    	 				this.html(this.html()+"<input id='gFilterInput' type='text' class='"+custom_input_class+"'/>");
    	 			}
    	 			else{
    	 				this.html(this.html()+"<input id='gFilterInput' type='text'/>");
    	 			}
    	 		}
    	 	}
    	 	else{
    	 		testing = $('#'+settings['inputId']);
    	 		if(testing.length == 0){
    	 			alert("You must add the inputId in the gFilter options. Thank you.");
    	 		}
    	 	}
    	 	
    	 	if(settings['displayMsg']){
    	 		msgHtml = "<p id='gFilterInputMessage' style='display:none'>"+settings['msg']+"</p>";
    	 		this.html(this.html()+msgHtml);
    	 	}
    	 	
    	 	obj = this;
    	 	$('#'+settings['inputId']).keyup(function(){
    	 		//Start filtering
        	 	searchFor = $('#'+settings['inputId']).val().toLowerCase();
        	 	//If the input field is empty - Show everything.
        	 	if($.trim(searchFor).length == 0){
        	 		if(settings['animate']){
        	 			obj.children(settings['filter']).slideDown();
        	 		}
        	 		else{
        	 			obj.children(settings['filter']).show();
        	 		}
        	 		if(settings['colorResults']){
	        	 		var re = new RegExp('<font id="gFilterSearchResultTxt" color="red">(.*)</font>', 'i');
	        	 		$('font[id=gFilterSearchResultTxt]').each(function(){
	        	 			this.outerHTML = (this.outerHTML.replace(re, '$1'));
	        	 		});
        	 		}
        	 	}
        	 	else{
        	 		//Filter results
        	 		var currChildren = obj.children(""+settings['filter']);
	        	 	var filtered = currChildren.filter(function(){
	        	 		currText = $(this).text().toLowerCase();
	        	 		test = currText.indexOf(searchFor) == -1;
	        	 		if(!test){
	        	 			//Highlight if found
	        	 			if(settings['colorResults']){
	        	 				var re = new RegExp("<\/?\w+((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>("+searchFor+")<\/?\w+((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>","gim");
	        	 				this.innerHTML = (this.innerHTML.replace(re, '<font id="gFilterSearchResultTxt" color="red">$5</font>'));
	        	 			}
	        	 			if(settings['animate']){
	        	 				$(this).slideDown();
	        	 			}
	        	 			else{
	        	 				$(this).show();
	        	 			}
	        	 		}
	        	 		return test;
	        	 	});
	        	 	
	        	 	if(settings['animate']){
	        	 		filtered.slideUp();
	        	 	}
	        	 	else{
	        	 		filtered.hide();
	        	 	}
        	 	}
        	 	if(settings['displayMsg']){
	        	 	if(!(obj.children(""+settings['filter']).is(':visible'))){
	        	 		if(settings['animate']){
	        	 			$('#gFilterInputMessage').slideDown();
	        	 		}
	        	 		else{
	        	 			$('#gFilterInputMessage').show();
	        	 		}
	        	 	}
	        	 	else{
	        	 		if(settings['animate']){
	        	 			$('#gFilterInputMessage').slideUp();
	        	 		}
	        	 		else{
	        	 			$('#gFilterInputMessage').hide();
	        	 		}
	        	 	}
        	 	}
    	 	});
    	} //end of init
    };
    
    $.fn.gFilter = function(method) {
    	methods.init.apply( this, arguments );
    };
})( jQuery );